package com.mata.worktime.configuration.shiro.service;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.mata.worktime.entity.user.User;
import com.mata.worktime.enums.ResultEnum;
import com.mata.worktime.exception.BaseException;

import javax.annotation.PostConstruct;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * 登录密码方法
 * 
 * @author earthchen
 */
@Component
public class PasswordService
{

    @Autowired
    private CacheManager cacheManager;

    private Cache<String, AtomicInteger> loginRecordCache;

    @Value(value = "${user.password.maxRetryCount}")
    private String maxRetryCount;

    @PostConstruct
    public void init()
    {
        loginRecordCache = cacheManager.getCache("loginRecordCache");
    }

    public void validate(User user, String password)
    {
        String loginName = user.getLoginName();

        AtomicInteger retryCount = loginRecordCache.get(loginName);

        if (retryCount == null)
        {
            retryCount = new AtomicInteger(0);
            loginRecordCache.put(loginName, retryCount);
        }
        if (retryCount.incrementAndGet() > Integer.valueOf(maxRetryCount).intValue())
        {
            throw new BaseException(ResultEnum.USER_PASSWORD_RETRY_LIMIT_EXCEED);
        }

        if (!matches(user, password))
        {
            loginRecordCache.put(loginName, retryCount);
            throw new BaseException(ResultEnum.USERNAME_PASSWORD_ERROR);
        }
        else
        {
            clearLoginRecordCache(loginName);
        }
    }

    public boolean matches(User user, String newPassword)
    {
        return user.getPassword().equals(encryptPassword(user.getLoginName(), newPassword, user.getSalt()));
    }

    public void clearLoginRecordCache(String username)
    {
        loginRecordCache.remove(username);
    }

    public String encryptPassword(String username, String password, String salt)
    {
        return new Md5Hash(username + password + salt).toHex().toString();
    }

    public static void main(String[] args)

    {
        String l = new Md5Hash("zengtao123456dd15d4").toHex();
        System.out.println(l);
//        System.out.println(new PasswordService().encryptPassword("admin", "", "111111"));
//        System.out.println(new PasswordService().encryptPassword("ry", "admin123", "222222"));
//        System.out.println(new PasswordService().encryptPassword("董事长", "admin123", "222222"));
//        // fc3785f8286def750ff90abe5cd1bcb8
//        System.out.println(new PasswordService().encryptPassword("项目经理", "admin123", "222222"));
//        // e0ef9c30651c041a6532d992cbdae2fc
//        System.out.println(new PasswordService().encryptPassword("员工", "admin123", "222222"));
//        // 18ea5c7e3ff33642fca9fe2a03198467
//        System.out.println(new PasswordService().encryptPassword("中心经理", "admin123", "222222"));
//        // b955d06a4c59ab5e93f57f4db6113d38
    }
}
